package com.mo.lib.utils

import android.content.Intent
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import com.dylanc.longan.topActivity
import com.mo.lib.utils.data.isEmpty
import com.wildma.pictureselector.PictureSelectUtils

/**
 * @ author：mo
 * @ data：2022/5/7:16:37
 * @ 功能：
 */
/**媒体回调onActivityResult处理        block：type==0==取消   type==1==失败 */
fun mediaResult(requestCode: Int, resultCode: Int, data: Intent?, block: (type: Int, path: String) -> Unit) {
    data?.let {
        if (resultCode == AppCompatActivity.RESULT_CANCELED && (requestCode == PictureSelectUtils.GET_BY_ALBUM || requestCode == PictureSelectUtils.GET_BY_CAMERA || requestCode == PictureSelectUtils.CROP)) { //选择图片中途按返回键
            block.invoke(0, "")
        } else {
            when (requestCode) {
                PictureSelectUtils.GET_BY_CAMERA -> { //拍照
                    val picturePath = PictureSelectUtils.onActivityResult(topActivity, requestCode, resultCode, data, false, 0, 0, 0, 0)
                    if (!isEmpty(picturePath)) {
                        val degree = getRotateDegree(picturePath)
                        if (degree != 0) {
                            saveFile(picturePath, getBitmapForRotate(BitmapFactory.decodeFile(picturePath), degree))
                        }
                    }
                    block.invoke(PictureSelectUtils.GET_BY_CAMERA, picturePath)
                }
                PictureSelectUtils.GET_BY_ALBUM  -> {
                    block.invoke(PictureSelectUtils.GET_BY_ALBUM, PictureSelectUtils.onActivityResult(topActivity, requestCode, resultCode, data, false, 200, 200, 1, 1))
                }
                else                             -> {
                }
            }
        }
    }
}